package com.hhp.cloud.shop.auth.security.core.userdetails.member;

import cn.hutool.core.lang.TypeReference;
import com.hhp.cloud.shop.auth.feign.MemberFeignService;
import com.hhp.cloud.shop.auth.vo.MemberAuthVo;
import com.hhp.cloud.shop.common.core.enums.AuthenticationIdentityEnum;
import com.hhp.cloud.shop.common.core.enums.AuthenticationMethodEnum;
import com.hhp.cloud.shop.common.core.enums.BizCodeEnume;
import com.hhp.cloud.shop.common.core.result.R;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Description: 会员用户认证服务
 * @author: hhp
 * @date: 2021/11/23 16:52
 */
@Service("memberUserDetailsService")
@RequiredArgsConstructor
public class MemberUserDetailsServiceImpl implements UserDetailsService {

    private MemberFeignService memberFeignService;

    @Override
    public UserDetails loadUserByUsername(String username) {
        return null;
    }


    /**
     * 手机号码认证方式
     *
     * @param mobile
     * @return
     */
    public UserDetails loadUserByMobile(String mobile) {
        MemberUserDetails userDetails = null;
        R<MemberAuthVo> result = memberFeignService.loadUserByMobile(mobile);
        if (R.ok().getCode() == result.getCode()) {
            MemberAuthVo member = result.getData(new TypeReference<MemberAuthVo>() {});
            if (null != member) {
                userDetails = new MemberUserDetails(member);
                userDetails.setAuthenticationIdentity(AuthenticationIdentityEnum.MOBILE.getValue());   // 认证身份标识:mobile
            }
        }
        if (userDetails == null) {
            throw new UsernameNotFoundException(BizCodeEnume.USER_NOT_EXIST.getMsg());
        } else if (!userDetails.isEnabled()) {
            throw new DisabledException("该账户已被禁用!");
        } else if (!userDetails.isAccountNonLocked()) {
            throw new LockedException("该账号已被锁定!");
        } else if (!userDetails.isAccountNonExpired()) {
            throw new AccountExpiredException("该账号已过期!");
        }
        return userDetails;
    }

    /**
     * openid 认证方式
     *
     * @param openId
     * @return
     */
    public UserDetails loadUserByOpenId(String openId) {
        MemberUserDetails userDetails = null;
        R<MemberAuthVo> result = memberFeignService.loadUserByOpenId(openId);
        if (R.ok().getCode() == result.getCode()) {
            MemberAuthVo member = result.getData(new TypeReference<MemberAuthVo>() {});
            if (null != member) {
                userDetails = new MemberUserDetails(member);
                userDetails.setAuthenticationIdentity(AuthenticationIdentityEnum.OPENID.getValue());   // 认证身份标识:openId
            }
        }
        if (userDetails == null) {
            throw new UsernameNotFoundException(BizCodeEnume.USER_NOT_EXIST.getMsg());
        } else if (!userDetails.isEnabled()) {
            throw new DisabledException("该账户已被禁用!");
        } else if (!userDetails.isAccountNonLocked()) {
            throw new LockedException("该账号已被锁定!");
        } else if (!userDetails.isAccountNonExpired()) {
            throw new AccountExpiredException("该账号已过期!");
        }
        return userDetails;
    }

}
